//
//  IMViewController.m
//  ipjsua
//
//  Created by Andy on 16/5/24.
//  Copyright © 2016年 Teluu. All rights reserved.
//

#import "IMViewController.h"

#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVAudioPlayer.h>
#import <MediaPlayer/MediaPlayer.h>
#import "CustemIMView.h"

#import "IMService.h"

@interface IMViewController ()<UITextFieldDelegate, CustemIMViewDelegate>
@property (weak, nonatomic) IBOutlet UITextField *serverTextFiled;
@property (weak, nonatomic) IBOutlet UITextField *nameTextFiled;
@property (weak, nonatomic) IBOutlet UITextField *pwdTextFiled;
@property (weak, nonatomic) IBOutlet UILabel *loginStatusLable;
@property (weak, nonatomic) IBOutlet UITextField *remoteTextFiled;
@property (weak, nonatomic) IBOutlet UITextView *recvMessageTextFiled;
@property (weak, nonatomic) IBOutlet UITextField *sendMessageTextFiled;
@property (nonatomic, strong) UIView * callView;
@property (nonatomic, strong) UIView * localView;

@property(nonatomic,strong) UIButton *refuseButton;
@property(nonatomic,strong) UIButton *acceptButton;
@property (nonatomic, strong) CustemIMView * custemIMView;


- (IBAction)loginAcion;
- (IBAction)sendAction;
- (IBAction)audioActio;
- (IBAction)videoAction;
- (IBAction)logoutAction;
@end

@implementation IMViewController
static IMViewController *IMvc =nil;

+(IMViewController *)shareInstance
{
    static IMViewController * instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[IMViewController alloc] initWithNibName:@"IMViewController" bundle:nil];
    });
    return instance;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [[IMService sharedInstance] IM_InitPJSIP];
//    [[IMService sharedInstance] IM_ShowAllDevList];
//    [[IMService sharedInstance] IM_GetAllCodecInfo];
    
    [[IMService sharedInstance] IM_SetCodecPriorityWithcodec:@"PCMA/8000" prio:0];
    [[IMService sharedInstance] IM_SetCodecPriorityWithcodec:@"PCMU/8000" prio:0];
    [[IMService sharedInstance] IM_SetCodecPriorityWithcodec:@"GSM/8000"  prio:0];
    [[IMService sharedInstance] IM_SetCodecPriorityWithcodec:@"iLBC/8000" prio:0];
    [[IMService sharedInstance] IM_SetCodecPriorityWithcodec:@"speex/16000" prio:0];
    [[IMService sharedInstance] IM_SetCodecPriorityWithcodec:@"speex/32000" prio:0];

//    [[IMService sharedInstance] IM_GetAllCodecInfo];
    [[IMService sharedInstance] IM_GetVideoCodecList];
    
    _custemIMView = [[CustemIMView alloc] init];
    _custemIMView.delegate = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/**
 * IM登录
 */
- (IBAction)loginAcion {
    NSString *server = self.serverTextFiled.text;
    NSString *name   = self.nameTextFiled.text;
    NSString *pwd    = self.pwdTextFiled.text;
    
    [[IMService sharedInstance] IM_RegisterWithServer:server name:name password:pwd];
}

/**
 * 退出
 */
- (IBAction)logoutAction {
    if([[IMService sharedInstance] IM_DeleteCurAcc])
        self.loginStatusLable.text = @"删除当前账号成功";
    else
        self.loginStatusLable.text = @"删除当前账号失败";
}

/**
 * 发送消息
 */
- (IBAction)sendAction {
    NSString *uri = self.remoteTextFiled.text;
    NSString *message = self.sendMessageTextFiled.text;
    
    [[IMService sharedInstance] IM_SendMSGWithuri:uri message:message];
}

/**
 * 语音呼叫
 */
- (IBAction)audioActio {
    //获取对方账号并呼叫对方
    NSString *uri = self.remoteTextFiled.text;
    [[IMService sharedInstance] IM_MakeAudioWithuri:uri];
    [_custemIMView showIMCallViewWithIMType:IMTypeAudio];
}


/**
 * 视频呼叫
 */
- (IBAction)videoAction {
//    [[IMService sharedInstance] IM_PreViewOnWithid:2];    
    NSString *uri = self.remoteTextFiled.text;
    [[IMService sharedInstance] IM_MakeVideoWithuri:uri];
    [_custemIMView showIMCallViewWithIMType:IMTypeVideo];
    
}


/**
 * 输入框回车结束
 */
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}


/**
 * 发送IM正在输入消息
 */
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (textField == self.sendMessageTextFiled) {
        NSString *uri = self.remoteTextFiled.text;
        [[IMService sharedInstance] IM_StartTypingWithuri:uri];
    }
    
    return YES;
}

/**
 * 发送IM停止输入消息
 */
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    if (textField == self.sendMessageTextFiled) {
        NSString *uri = self.remoteTextFiled.text;
        [[IMService sharedInstance] IM_StopTypingWithuri:uri];
    }
    
    return YES;
}


/**
 * 接收登录状态输入消息
 */
-(void)recvNotifyWithstr:(NSString *)str
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [IMViewController shareInstance].loginStatusLable.text = str;
        
    });
}

/**
 * 接收IM消息
 */
-(void)recvMessageWithstr:(NSString *)str
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [IMViewController shareInstance].recvMessageTextFiled.text = str;
        
    });
}

/**
 * 收到语音呼叫
 */
-(void)recvAudioCallWithfrom:(NSString *)from to:(NSString *)to
{
    [_custemIMView showIMCallViewWithIMType:IMTypeAudio];
}

/**
 * 收到视频呼叫
 */
-(void)recvVideoCallWithfrom:(NSString *)from to:(NSString *)to
{
    [_custemIMView showIMCallViewWithIMType:IMTypeVideo];
}


/**
 * Invite连接断开
 */
-(void)InviteDisconnectWithcallid:(int)callid code:(int)code status:(NSString *)status
{
    [[IMService sharedInstance] IM_PreViewOffWithid:2];
    [[IMService sharedInstance] IM_PreViewOffWithid:3];
    [_custemIMView hideIMCallView];
}


/**
 * 远端视频显示
 */
-(void)showRemoteViewWithView:(UIView *)view
{
    [_custemIMView updateWithDistanceView:view];
}


/**
 * 本地视频显示
 */
-(void)showlocaleViewWithView:(UIView *)view displayBigNative:(BOOL)displayBigNative
{
    [_custemIMView updateWithLocalView:view displayBigNative:displayBigNative];
}

- (void)refuseIMRequest
{
    [[IMService sharedInstance] IM_HangUp];
}

- (void)agreeIMRequest
{
    [[IMService sharedInstance] IM_AnswerCall];
}

@end

